<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>

        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="仓库管理">

            <div style="text-align: right; margin-bottom: 20px">
              <el-button icon="el-icon-plus" type="primary" size="small" @click="clickAdd(1, '0')">新增</el-button>
            </div>
            <el-table row-key="id" :data="list" border>
              <el-table-column label="序号" prop="name">
                <template #default="{ row }">
                  <!-- <i class="el-icon-receiving" /> -->
                  {{ row.id }}
                </template>
              </el-table-column>
              <el-table-column label="仓库名" prop="name" />
              <el-table-column label="地址" prop="address" />
              <el-table-column label="邮编" prop="postalCode" />
              <el-table-column label="收货人" prop="receivername" />
              <el-table-column label="电话" prop="phone" />
              <el-table-column label="操作"  >
                <template slot-scope="scope" fixed="right">
                  <el-button size="small" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <span>确定删除吗？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="deleteComfirm">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 弹框 -->
      <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
        <el-form v-loading="loading" label-width="90px">
          <el-form-item label="仓库名" prop="name">
            <el-input v-model="formData.name" placeholder="请输入仓库名字" />
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="formData.address" placeholder="请输入仓库地址" />
          </el-form-item>
          <el-form-item label="邮编">
            <el-input v-model="formData.postalcode" placeholder="请输入邮编" />
          </el-form-item>

          <el-form-item label="收件人姓名">
            <el-input v-model="formData.receivername" placeholder="请输入姓名" />
          </el-form-item>

          <el-form-item label="收件人电话">
            <el-input v-model="formData.phone" placeholder="请输入收件人电话" />
          </el-form-item>
          <!-- <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
              active-text="启用"
              inactive-text="未启用"
            />
          </el-form-item> -->
          <!-- <el-table-column
              fixed="right"
              label="操作"
              width="100">
              <template slot-scope="scope">
                <el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
                <el-button type="text" size="small">编辑</el-button>
              </template>
            </el-table-column> -->
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确认</el-button>
          </div>
        </template>
      </el-dialog>

    </div>

    <!-- 分页 -->
    <div align="center">
      <el-pagination
        :current-page.sync="currentPage"
        layout="prev, pager, next, jumper"
        :total="list.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
// import { reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permission'
// import { tranListToTreeData } from '@/utils/index'
import { selectAllDep, insertDepository, deleteDep ,updateDep} from '@/api/depository'
export default {
  name: 'Permission',
  data() {
    return {
      dialogVisible: false,
      // 分页数据
      currentPage: 1,
      list: [],
      showDialog: false,
      formData: {
        // enVisible: '0', // 0不开启, 1开启 (注意: 不是布尔值, 所以将来需要处理switch)
        name: '', // 名称
        address: '',
        postalcode: '',
        receivername: '',
        phone: ''
        // code: '手机号码', // 账号
        // description: '1234567', // 密码
        // type: 'XXX', // 类型, 1一级访问权限, 2二级操作权限
        // pid: '' // 添加到哪个节点下, 树形结构渲染的, 点击某个节点时, 也能知道pid是什么
      },
      loading: false,
      permissionData: [] // 存放用于展示的权限列表数据 (树形结构)
    }
  },
  computed: {
    showTitle() {
      if (this.formData.id) {
        return '编辑'
      } else {
        return '添加'
      }
    }
  },
  mounted() {
    console.log('进入页面的钩子函数')
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      // const { data } = await reqGetPermissionList()
      //   // this.list = tranListToTreeData(data, '0')
      // console.log(111,this.list) // 处理过的树形结构的数据
      const data = await selectAllDep()
      this.list = data
    },
    clickAdd(type, pid) {
      this.showDialog = true
      // 点击显示弹框时, 立刻记录type, pid
      // this.formData.type = type
      // this.formData.pid = pid
    },
    closeDialog() {
      this.showDialog = false
      // 重置清空数据
      this.formData = {
        enVisible: '0', // 0不开启, 1开启 (注意: 不是布尔值, 所以将来需要处理switch)
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型, 1一级访问权限, 2二级操作权限
        pid: '' // 添加到哪个节点下, 树形结构渲染的, 点击某个节点时, 也能知道pid是什么
      }
    },
    // 点击弹框的确认按钮
    async clickSubmit() {
      // if (this.formData.name === '' || this.formData.code === '') {
      //   this.$message.error('仓库名和地址不能为空')
      //   return
      // }
      if (this.formData.id) {
        // 编辑操作
        const {postalcode,...formData} = this.formData
        await updateDep(formData)
      } else {
        // 添加操作
        await insertDepository(this.formData)
      }
      this.showDialog = false
      this.$message.success('操作成功')
      this.getPermissionList()
    },
    // 删除功能
    handleDelete(id) {
      this.deleteId = id
      this.dialogVisible = true
    },
    async deleteComfirm() {
      this.dialogVisible = false
      await deleteDep({
        id: this.deleteId
      })
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      this.getPermissionList()
    },
    handleEdit(row){
      console.log(row)
      // const {postalCode,...formData} = row
      // formData.postalcode = postalCode
      this.formData = row;
      this.formData.postalcode = row.postalCode
      this.showDialog = true
    },
    // 点击查看编辑按钮, 显示弹框
    clickEdit(id) {
      // 显示弹框
      this.showDialog = true
      this.loading = true
      // 发送请求, 获取数据, 进行回显
      // const { data } = await reqGetPermissionDetail(id)
      // this.formData = data

      this.loading = false
    },

    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    }
  }
}
</script>

<style>

</style>
